import React, { useEffect, useState, memo } from "react";
import { createForm } from "rc-form";
import { List, InputItem, Toast, WhiteSpace, Picker } from "antd-mobile";
import { openSelectAddress } from "@/tools/native/navigate.js";
import yhTools from "@/tools/yh";
import "./index.scss";

const job = [
	{
		label: "全职有薪",
		value: "00-全职有薪"
	},
	{
		label: "实习或兼职",
		value: "01-实习或兼职"
	},
	{
		label: "个体",
		value: "02-个体"
	},
	{
		label: "自由职业",
		value: "03-自由职业"
	}
	// {
	// 	label: "学生",
	// 	value: "04-学生"
	// }
];
const fieldPropsMap = {
	userAddressDetail: {
		rules: [
			{
				required: true,
				message: "详细地址不能为空"
			}
		]
	},
	companyName: {
		rules: [
			{
				required: true,
				message: "单位名称不能为空"
			}
		]
	},
	jobStatus: {
		rules: [
			{
				required: true,
				message: "就业状态不能为空"
			}
		]
	}
};

let address = {};
function isHide(value) {
	return value === "04-学生" || value === "02-个体" || value === "03-自由职业";
}
const OtherInfo = props => {
	const { form, values, getValuesFn } = props;
	const { getFieldProps, getFieldError, validateFields, setFieldsValue } = form;
	const [userAddress, setUserAddress] = useState("");
	const [companyName, setCompanyName] = useState(true);
	useEffect(() => {
		if (values) {
			setCompanyName(!isHide(values.jobStatus));
			values.jobStatus = [values.jobStatus];
			setFieldsValue(values);
			address.title = values.userAddress;
			setUserAddress(values.userAddress);
		}
	}, [values]);
	const onErrorClick = name => {
		const message = getFieldError(name).join("、");
		Toast.fail(message);
	};

	//打开地图
	const openMap = () => {
		openSelectAddress().then(res => {
			if (res.status === "1000") {
				const {
					// lat,
					// lng,
					// adcode,
					// province,
					// city,
					// district,
					title
					// address
				} = res.data;
				setUserAddress(title);
				setFieldsValue({ userAddressDetail: res.data.address });
				address.title = title;
			} else {
				Toast.info(res.data.message, 5);
			}
		});
	};
	const getValue = () => {
		return new Promise((reslove, reject) => {
			validateFields((err, values) => {
				if (err) {
					reject(err);
				} else {
					// if (yhTools.platform() == "android") {
					// 	if (!address.title) {
					// 		Toast.fail("现居地址不能为空");
					// 		return reject("");
					// 	}
					// }
					values.userAddress = address.title || "";
					values.jobStatus = values.jobStatus[0];
					reslove(values);
				}
			});
		});
	};
	const onChange = values => {
		const value = values[0];
		if (isHide(value)) {
			setCompanyName(false);
		} else {
			setCompanyName(true);
		}
	};
	useEffect(() => {
		getValuesFn(getValue);
	}, []);
	return (
		<div className="otherInfo-linkman-wrapper">
			<WhiteSpace />
			<List>
				{/* {yhTools.platform() == "android" ? (
					<>
						<List.Item
							extra={userAddress || "请选择现居地址"}
							arrow="horizontal"
							onClick={openMap}
						>
							现居地址
						</List.Item>
						<InputItem
							placeholder="请输入详细地址"
							error={getFieldError("userAddressDetail")}
							onErrorClick={() => onErrorClick("userAddressDetail")}
							{...getFieldProps(
								"userAddressDetail",
								fieldPropsMap.userAddressDetail
							)}
						>
							<i></i>
						</InputItem>
					</>
				) : (
					""
				)} */}
				<Picker
					cols={1}
					title="请选择"
					extra="请选择就业状态"
					data={job}
					error={getFieldError("jobStatus")}
					onErrorClick={() => onErrorClick("jobStatus")}
					{...getFieldProps("jobStatus", fieldPropsMap.jobStatus)}
					onOk={onChange}
				>
					<List.Item arrow="horizontal">就业状态</List.Item>
				</Picker>

				{companyName && (
					<InputItem
						placeholder="请填写单位名称"
						error={getFieldError("companyName")}
						onErrorClick={() => onErrorClick("companyName")}
						{...getFieldProps("companyName", fieldPropsMap.companyName)}
					>
						单位名称
					</InputItem>
				)}
			</List>
		</div>
	);
};
export default createForm()(memo(OtherInfo));
